IF exists(SELECT * FROM dbo.sysobjects WHERE name='fl_LeegHulptabel' AND xtype='P') 
BEGIN
	DROP PROCEDURE fl_LeegHulptabel
END
GO

CREATE  procedure fl_LeegHulptabel 
(@klas varchar(5),@semester int)
as
  
create table #vakken(vak char(2))

insert into #vakken(vak)
select distinct vakcode 
   from leraars..ambtsverdeling 
   where klas = @klas
declare @vakVolgNr int
declare @vak char(2)
declare @j int
declare @sqlZin varchar(5000)

declare deCursor cursor local for
select vak
from #vakken
order by vak
for read only

set @vakVolgNr = 1
open deCursor
fetch next from deCursor into @vak
  while (@@fetch_status=0)
    begin
      set @j = 1
      while @j < 4 
      begin
        set @sqlZin = 'insert into fl_ToetsLlnInschrijving(inschrijvingsNr,' +
	  'periode,taak,toets,semester,vak) ' +
      	  'select inschrijvingsNr,''' + convert(char(1),@j) + ''',' +
 	  'V' + convert(varchar(2),@vakVolgNr) + 'Taak' + convert(varchar(1),@j) + ',' +
 	  'V' + convert(varchar(2),@vakVolgNr) + 'Toets' + convert(varchar(1),@j) + 
	  ',''' + convert(char(1),@semester) + ''',''' + @vak + ''' ' +
	  ' from fl_ToetsinvoerTakenHulpTabel'
	execute(@sqlZin)
        set @j = @j + 1
      end
      fetch next from deCursor into @vak
    end
close deCursor
deallocate deCursor